<?php
namespace app\system\model;
use \think\Model;
use think\Db;
use app\member\model\Member;

class SuperAdmin extends Base{
    /**
     * @param $data
     * @return bool|false|int
     * 添加或者编辑超级管理员
     */
    public function updata_admin($data){
        $this->insert = ['status'=>0,'addtime'=> request()->time(),'site_type'=>0];
        $ok = false;
        try {
            if ( isset( $data['id'] ) ) {
                $ok = $this->allowField(true)->isUpdate(true)->data($data,true)->save();
            } else {
                $ok = $this->allowField(true)->isUpdate(false)->data($data,true)->save();
            }
            if(false !== $ok) self::del_cache();
            return $ok;
        }catch(\Exception $e){
            return false;
        }
    }

    /**
     * @param array $data
     * @return bool|int
     * 删除超级管理员
     */
    public function del_admin($data=[]){
        try {
            $ok =  $this->where(['id'=>['in',$data['id']]])->delete();
            if(false !== $ok) self::del_cache();
            return $ok;
        }catch(\Exception $e){
            return false;
        }
    }

    /**
     * @param string $where
     * @param $param
     * @return bool
     * 管理员列表
     */
    public function admin_list($where='1',$param){
        try {
            $list['total'] =  $this->where($where)->count();
            $list['list'] = $this->where($where)->order($param['order_str'])->page($param['page'],$param['rows'])->select();
            return $list;
        }catch(\Exception $e){
            return false;
        }
    }

    /**
     * @param $web_id
     * @return int
     * 检测是否管理员
     */
    static public function check_super_admin($web_id){
        $admin_list = cache('super_admin','',get_cache_path(false));
        if(empty($admin_list)){
            $admin_list = Db::name('SuperAdmin')->where(['status'=>1])->column('web_id');
        }
        if(!empty($admin_list) && in_array($web_id,$admin_list)){
            return 1;
        }
        return 0;
    }

    /**
     * 删除缓存
     */
    private function del_cache(){
        cache('super_admin',null,get_cache_path(false));
    }
    public function detail($where){
        try {
            $list =  $this->where($where)->find();
            return $list;
        }catch(\Exception $e){
            return false;
        }
    }

}